package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import com.project.util.MD5Encryptor;
import com.project.util.SnowIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;

@Transactional
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;
    @Override
    public UserBean login(String userName, String password) {
        
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userName",userName);
        qw.eq("password",password);
        return mapper.selectOne(qw);
    }
    
    @Override
    public Double findMoney(Long userNum) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",userNum);
        return mapper.selectOne(qw).getMoney();
    }
    @Override
    public UserBean addUser(UserBean userBean) throws NoSuchAlgorithmException {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userName",userBean.getUserName());
        UserBean user=mapper.selectOne(qw);
        if (user==null){
            UserBean u=new UserBean();
            u.setUserName(userBean.getUserName());
            u.setPassword(MD5Encryptor.encrypt(userBean.getPassword()));
            u.setPhone(userBean.getPhone());
            u.setUserNum(SnowIdUtils.uniqueLong());
            u.setRealName(userBean.getRealName());
            u.setSex(userBean.getSex());
            u.setApproveStatus(3);//认证状态 3-未认证
            u.setFreezeStatus(1);//冻结状态 1-未冻结
            mapper.insert(u);
            return null;
        }else {
            return user;
        }
        
        
    }
    
    @Override
    public void updateUser(UserBean user) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",user.getUserNum());
        UserBean userBean=mapper.selectOne(qw);
        userBean.setCompany(user.getCompany());
        userBean.setIDImg(user.getIDImg());
        userBean.setQuaImg(user.getQuaImg());
        mapper.updateById(userBean);
    }
    
    @Override
    public void updateCustomtStatus(int status, Long userNum) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",userNum);
        UserBean userBean=mapper.selectOne(qw);
        userBean.setFreezeStatus(status);
        mapper.updateById(userBean);
    }
    @Override
    public UserBean findUserNum(Long userNum) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",userNum);
        return mapper.selectOne(qw);
        
    }
    @Override
    public void updateStatus(Long userNum,int status) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",userNum);
        UserBean user= mapper.selectOne(qw);
        user.setApproveStatus(status);
        mapper.updateById(user);
    }
    
    @Override
    public void updateDiscount(Long userNum, double discount,Double totalPrice) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("userNum",userNum);
        UserBean user= mapper.selectOne(qw);
        user.setDiscount(discount);
        user.setMoney(totalPrice);
        mapper.updateById(user);
    }
}
